<?php

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/tcms_registry_keys.class.php');
require_once($WSC->get_site_inc_dir().'modules/registry_db/registry_key_db.class.php');


class TCMS_Settings_Images_DBC extends Tangra_Class implements I_DB_Storable {
	private $id;
	private $allowed_extensions;
	private $to_size_x;
	private $to_size_y;
	private $jpg_quality;
	private $bgcolor;
	private $keep_original;
	private $thumbnail1_size_x;
	private $thumbnail1_size_y;
	private $thumbnail2_size_x;
	private $thumbnail2_size_y;
	private $maximum_size;
	private $maximum_size_x;
	private $maximum_size_y;
	private $minimum_ratio_xy;
	private $maximum_ratio_xy;
	private $watermark_image = false;
	private $watermark_position = 0;
	private $upload_path;


	//fake
	public function set_id($id) {
	}


	//fake
	public function get_id() {
		return 1;
	}


	public function save(DB_Connection $dbc) {
		$this->allowed_extensions->save($dbc);
		$this->to_size_x->save($dbc);
		$this->to_size_y->save($dbc);
		$this->jpg_quality->save($dbc);
		$this->bgcolor->save($dbc);
		$this->keep_original->save($dbc);
		$this->thumbnail1_size_x->save($dbc);
		$this->thumbnail1_size_y->save($dbc);
		$this->thumbnail2_size_x->save($dbc);
		$this->thumbnail2_size_y->save($dbc);
		$this->maximum_size->save($dbc);
		$this->maximum_size_x->save($dbc);
		$this->maximum_size_y->save($dbc);
		$this->minimum_ratio_xy->save($dbc);
		$this->maximum_ratio_xy->save($dbc);
		$this->watermark_image->save($dbc);
		$this->watermark_position->save($dbc);
		$this->upload_path->save($dbc);

		return 1;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);


		$allowed_extensions_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_ALLOWED_EXTENSIONS);
		if ($allowed_extensions_id) {
			$allowed_extensions = new Registry_Key_DB();
			$allowed_extensions->load_by_id($dbc, $allowed_extensions_id);
			$this->allowed_extensions = $allowed_extensions;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_ALLOWED_EXTENSIONS);
		}

		$to_size_x_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_TO_SIZE_X);
		if ($to_size_x_id) {
			$to_size_x = new Registry_Key_DB();
			$to_size_x->load_by_id($dbc, $to_size_x_id);
			$this->to_size_x = $to_size_x;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_TO_SIZE_X);
		}

		$to_size_y_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_TO_SIZE_Y);
		if ($to_size_y_id) {
			$to_size_y = new Registry_Key_DB();
			$to_size_y->load_by_id($dbc, $to_size_y_id);
			$this->to_size_y = $to_size_y;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_TO_SIZE_Y);
		}

		$jpg_quality_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_JPG_QUALITY);
		if ($jpg_quality_id) {
			$jpg_quality = new Registry_Key_DB();
			$jpg_quality->load_by_id($dbc, $jpg_quality_id);
			$this->jpg_quality = $jpg_quality;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_JPG_QUALITY);
		}

		$bgcolor_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_BGCOLOR);
		if ($bgcolor_id) {
			$bgcolor = new Registry_Key_DB();
			$bgcolor->load_by_id($dbc, $bgcolor_id);
			$this->bgcolor = $bgcolor;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_BGCOLOR);
		}

		$keep_original_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_KEEP_ORIGINAL);
		if ($keep_original_id) {
			$keep_original = new Registry_Key_DB();
			$keep_original->load_by_id($dbc, $keep_original_id);
			$this->keep_original = $keep_original;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_KEEP_ORIGINAL);
		}


		$thumbnail1_size_x_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL1_SIZE_X);
		if ($thumbnail1_size_x_id) {
			$thumbnail1_size_x = new Registry_Key_DB();
			$thumbnail1_size_x->load_by_id($dbc, $thumbnail1_size_x_id);
			$this->thumbnail1_size_x = $thumbnail1_size_x;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL1_SIZE_X);
		}

		$thumbnail1_size_y_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL1_SIZE_Y);
		if ($thumbnail1_size_y_id) {
			$thumbnail1_size_y = new Registry_Key_DB();
			$thumbnail1_size_y->load_by_id($dbc, $thumbnail1_size_y_id);
			$this->thumbnail1_size_y = $thumbnail1_size_y;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL1_SIZE_Y);
		}


		$thumbnail2_size_x_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL2_SIZE_X);
		if ($thumbnail2_size_x_id) {
			$thumbnail2_size_x = new Registry_Key_DB();
			$thumbnail2_size_x->load_by_id($dbc, $thumbnail2_size_x_id);
			$this->thumbnail2_size_x = $thumbnail2_size_x;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL2_SIZE_X);
		}

		$thumbnail2_size_y_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL2_SIZE_Y);
		if ($thumbnail2_size_y_id) {
			$thumbnail2_size_y = new Registry_Key_DB();
			$thumbnail2_size_y->load_by_id($dbc, $thumbnail2_size_y_id);
			$this->thumbnail2_size_y = $thumbnail2_size_y;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_THUMBNAIL2_SIZE_Y);
		}

		$maximum_size_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_SIZE);
		if ($maximum_size_id) {
			$maximum_size = new Registry_Key_DB();
			$maximum_size->load_by_id($dbc, $maximum_size_id);
			$this->maximum_size = $maximum_size;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_SIZE);
		}

		$maximum_size_x_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_SIZE_X);
		if ($maximum_size_x_id) {
			$maximum_size_x = new Registry_Key_DB();
			$maximum_size_x->load_by_id($dbc, $maximum_size_x_id);
			$this->maximum_size_x = $maximum_size_x;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_SIZE_X);
		}

		$maximum_size_y_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_SIZE_Y);
		if ($maximum_size_y_id) {
			$maximum_size_y = new Registry_Key_DB();
			$maximum_size_y->load_by_id($dbc, $maximum_size_y_id);
			$this->maximum_size_y = $maximum_size_y;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_SIZE_Y);
		}

		$minimum_ratio_xy_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_MINIMUM_RATIO_XY);
		if ($minimum_ratio_xy_id) {
			$minimum_ratio_xy = new Registry_Key_DB();
			$minimum_ratio_xy->load_by_id($dbc, $minimum_ratio_xy_id);
			$this->minimum_ratio_xy = $minimum_ratio_xy;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_MINIMUM_RATIO_XY);
		}

		$maximum_ratio_xy_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_RATIO_XY);
		if ($maximum_ratio_xy_id) {
			$maximum_ratio_xy = new Registry_Key_DB();
			$maximum_ratio_xy->load_by_id($dbc, $maximum_ratio_xy_id);
			$this->maximum_ratio_xy = $maximum_ratio_xy;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_MAXIMUM_RATIO_XY);
		}

		$watermark_image_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_WATERMARK_IMAGE);
		if ($watermark_image_id) {
			$watermark_image = new Registry_Key_DB();
			$watermark_image->load_by_id($dbc, $watermark_image_id);
			$this->watermark_image = $watermark_image;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_WATERMARK_IMAGE);
		}

		$watermark_position_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_WATERMARK_POSITION);
		if ($watermark_position_id) {
			$watermark_position = new Registry_Key_DB();
			$watermark_position->load_by_id($dbc, $watermark_position_id);
			$this->watermark_position = $watermark_position;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_WATERMARK_POSITION);
		}

		$upload_path_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES_UPLOAD_PATH);
		if ($upload_path_id) {
			$upload_path = new Registry_Key_DB();
			$upload_path->load_by_id($dbc, $upload_path_id);
			$this->upload_path = $upload_path;
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES_UPLOAD_PATH);
		}

		return 1;
	}
	
	
	public static function load_settings_flat(DB_Connection $dbc, $table_name = 'registry') {
		$ret = array();
		
		$images_key_id = Registry_Key_DB::key_exists($dbc, TCMS_Registry_Keys::KEY_TCMS_IMAGES);
		if ($images_key_id) {
			$sql = "select key_name, key_value from $table_name where parent = $images_key_id";
			$rez = $dbc->execute($sql);
			while($rez_obj = $rez->fetch_object()) {
				$ret[$rez_obj->KEY_NAME] = basename($rez_obj->KEY_VALUE);
			}
		} else {
			throw new Tangra_Module_Exception('Registry key missing: '.TCMS_Registry_Keys::KEY_TCMS_IMAGES);			
		}
		
		return $ret;
	}
	

	public function set_allowed_extensions($value) {
		$this->allowed_extensions->set_value($value);
	}


	public function get_allowed_extensions() {
		return $this->allowed_extensions->get_value();
	}


	public function set_to_size_x($value) {
		tangra_if_not_int_throw_e($value);
		$this->to_size_x->set_value($value);
	}


	public function get_to_size_x() {
		return $this->to_size_x->get_value();
	}


	public function set_to_size_y($value) {
		tangra_if_not_int_throw_e($value);
		$this->to_size_y->set_value($value);
	}


	public function get_to_size_y() {
		return $this->to_size_y->get_value();
	}


	public function set_jpg_quality($value) {
		tangra_if_not_int_throw_e($value);
		$this->jpg_quality->set_value($value);
	}


	public function get_jpg_quality() {
		return $this->jpg_quality->get_value();
	}


	public function set_bgcolor($value) {
		$this->bgcolor->set_value($value);
	}


	public function get_bgcolor() {
		return $this->bgcolor->get_value();
	}


	public function set_keep_original($value) {
		$this->keep_original->set_value($value);
	}


	public function get_keep_original() {
		return $this->keep_original->get_value();
	}


	public function set_thumbnail1_size_x($value) {
		tangra_if_not_int_throw_e($value);
		$this->thumbnail1_size_x->set_value($value);
	}


	public function get_thumbnail1_size_x() {
		return $this->thumbnail1_size_x->get_value();
	}


	public function set_thumbnail1_size_y($value) {
		tangra_if_not_int_throw_e($value);
		$this->thumbnail1_size_y->set_value($value);
	}


	public function get_thumbnail1_size_y() {
		return $this->thumbnail1_size_y->get_value();
	}


	public function set_thumbnail2_size_x($value) {
		tangra_if_not_int_throw_e($value);
		$this->thumbnail2_size_x->set_value($value);
	}


	public function get_thumbnail2_size_x() {
		return $this->thumbnail2_size_x->get_value();
	}


	public function set_thumbnail2_size_y($value) {
		tangra_if_not_int_throw_e($value);
		$this->thumbnail2_size_y->set_value($value);
	}


	public function get_thumbnail2_size_y() {
		return $this->thumbnail2_size_y->get_value();
	}

	public function set_maximum_size($value) {
		tangra_if_not_int_throw_e($value);
		$this->maximum_size->set_value($value);
	}


	public function get_maximum_size() {
		return $this->maximum_size->get_value();
	}


	public function set_maximum_size_x($value) {
		tangra_if_not_int_throw_e($value);
		$this->maximum_size_x->set_value($value);
	}


	public function get_maximum_size_x() {
		return $this->maximum_size_x->get_value();
	}


	public function set_maximum_size_y($value) {
		tangra_if_not_int_throw_e($value);
		$this->maximum_size_y->set_value($value);
	}


	public function get_maximum_size_y() {
		return $this->maximum_size_y->get_value();
	}


	public function set_minimum_ratio_xy($value) {
		$this->minimum_ratio_xy->set_value($value);
	}


	public function get_minimum_ratio_xy() {
		return $this->minimum_ratio_xy->get_value();
	}


	public function set_maximum_ratio_xy($value) {
		$this->maximum_ratio_xy->set_value($value);
	}


	public function get_maximum_ratio_xy() {
		return $this->maximum_ratio_xy->get_value();
	}


	public function set_watermark_image($value) {
		$this->watermark_image->set_value($value);
	}


	public function get_watermark_image() {
		return $this->watermark_image->get_value();
	}


	public function set_watermark_position($value) {
		tangra_if_not_int_throw_e($value);
		$this->watermark_position->set_value($value);
	}


	public function get_watermark_position() {
		return $this->watermark_position->get_value();
	}


	public function set_upload_path($value) {
		$this->upload_path->set_value($value);
	}


	public function get_upload_path() {
		return $this->upload_path->get_value();
	}
}